﻿using System.Windows.Forms;

namespace GeneralMethodClass.Windows
{
    /// <summary>
    /// 消息对话框
    /// </summary>
    public class MsgBox
    {
        private static string _defaultTitle;

        /// <summary>
        /// 消息框缺省标题
        /// </summary>
        public static string DefaultTitle { get { return _defaultTitle; } set { _defaultTitle = value; } }

        /// <summary>
        /// 显示指定文本的消息框
        /// </summary>
        /// <param name="text">要显示的文本</param>
        public static void Show(string text)
        {
            Show(text, string.IsNullOrEmpty(_defaultTitle) ? "信息" : _defaultTitle);
        }

        /// <summary>
        /// 显示指定文本和标题的消息框
        /// </summary>
        /// <param name="text">要显示的文本</param>
        /// <param name="title">消息框的标题</param>
        public static void Show(string text, string title)
        {
            if (title.Contains("错误") || title.Contains("失败"))
                MessageBox.Show(text, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
            else if (title.Contains("警告"))
                MessageBox.Show(text, title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else if (title.Contains("消息") || title.Contains("信息") || title.Contains("提示"))
                MessageBox.Show(text, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
            else if (title.Contains("询问"))
                MessageBox.Show(text, title, MessageBoxButtons.OK, MessageBoxIcon.Question);
            else
                MessageBox.Show(text, title, MessageBoxButtons.OK, MessageBoxIcon.None);
        }

        /// <summary>
        /// 显示包含“是”和“否”按钮的询问消息框
        /// </summary>
        /// <param name="text">要显示的文本</param>
        /// <returns></returns>
        public static bool ShowYesNo(string text, string title)
        {
            if (title.Contains("错误") || title.Contains("失败"))
                return MessageBox.Show(text, title, MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes;
            else if (title.Contains("警告"))
                return MessageBox.Show(text, title, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes;
            else if (title.Contains("消息") || title.Contains("信息") || title.Contains("提示"))
                return MessageBox.Show(text, title, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes;
            else if (title.Contains("询问"))
                return MessageBox.Show(text, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
            else
                return MessageBox.Show(text, title, MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes;
        }

        /// <summary>
        /// 显示包含“是”和“否”按钮的询问消息框
        /// </summary>
        /// <param name="text">要显示的文本</param>
        /// <param name="title">消息框的标题</param>
        /// <returns></returns>
        public static bool ShowYesNo(string text, string title)
        {
            return MessageBox.Show(text, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
        }
    }
}
